package _1request;

import com.sun.net.httpserver.HttpServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

@WebServlet("/test1")
public class Test1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw=resp.getWriter();

//        //1.请求行
//        pw.println("请求方式："+req.getMethod()+"<br>");
//        pw.println("请求地址："+req.getRequestURI()+"<br>");
//        pw.println("请求地址："+req.getRemoteAddr()+"<br>");
//        pw.println("请求协议："+req.getProtocol()+"<br>");
//
//        //2.请求头
//        pw.println("请求客户端ip："+req.getRemoteAddr()+"<br>");
//        pw.println("请求头"+req.getHeaderNames()+"<br>");
//
//        Enumeration<String> keys=req.getHeaderNames();
//        while (keys.hasMoreElements())
//        {
//            String key=keys.nextElement();
//            pw.println(key+":"+req.getHeader(key)+"<br>");
//        }

        //3 参数信息

        pw.println(req.getParameter("username")+"<br>");
        pw.println(req.getParameter("password")+"<br>");

        Map<String ,String[]> maps=req.getParameterMap();

        for (Map.Entry<String, String[]> e:
        maps.entrySet()){

            pw.println(e.getKey()+":"+ Arrays.toString(e.getValue()));
        }

        //4.域对象

        req.setAttribute("name2","lisi");
        pw.print(req.getAttribute("name2"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
